#-*-coding:utf8 -*-
__version__=0.2
__author__ ="Alycat"

from Tkinter import *
import json
import sys
sys.path.append("D:\NoteSlate\Module")
# This function used to load josn file.
def loadJson(file=None):
          loadFile=open(file,'r')
          jsonObj=json.loads(loadFile.read(),"utf-8")
          return jsonObj

DEFAULT_PATH="../Config/"
MENU="menu"
FATHER="father"
TYPE="type"
VALUE="value"
TITLE="title"
CONF="conf"
INDEX="index"
GEOMETRY="geometry"
CMD='command'
BTN='button'
LAYOUT='layout'
ITM='item'
FILE='file'
class View():

     def __init__(self):
          self.keyset={}
          self.tk=Tk()
          self.initUI()
          self.tk.mainloop()

     '''
          This function init the gui compentent,include the menubar,textarea and statusBar.
     '''

     def initUI(self):
          default=loadJson(DEFAULT_PATH+"default.json")
          self.tk.title(default[TITLE])
          self.tk.geometry(default[GEOMETRY])
          self.createMenu(default[MENU])
          self.createCompentent(default[ITM])

     '''
          Create Menu
     '''
     def createMenu(self,menu_file=None):
          if menu_file is None:
               return -1
          menu_info=loadJson(DEFAULT_PATH+menu_file)
          self.menu=menubar=Menu(self.tk)
          for menu in menu_info:
               if menu.has_key(CONF):
                    menuItem=Menu(menubar,menu[CONF])
                    menu[VALUE][MENU]=menuItem
                    menubar.insert(menu[INDEX], menu[TYPE],menu[VALUE])
               elif menu.has_key(FATHER):
                    menuButton=menubar.children[menu[FATHER]]
                    menuButton.insert(menu[INDEX], menu[TYPE],menu[VALUE])
                    if menu[CMD] is not None and len(menu[CMD])>0:
                         mod_func=menu[CMD].split('.')
                         mod=__import__(mod_func[0])
                         func=getattr(mod,mod_func[1])
                         menuButton.entryconfig(menu[INDEX],{CMD:func})
                         # Set key entry
                         self.keyset[menu[BTN]]=func               
          self.tk.config(menu=menubar)
          self.bindKeys()

     def bindKeys(self):
          for key in self.keyset.viewkeys():
               if len(key) > 1: 
                    self.tk.bind(key,self.handlerAdaptor(self.handler,key))

     def handlerAdaptor(self,fun, *kwds):
          return lambda event,fun=fun,kwds=kwds: fun(event, *kwds)

     def handler(self,event,key):
          func=self.keyset[key]
          func() 
  
     def CompententDetail(self,widget=None,wid_file=None):
          print widget,wid_file
          wids=loadJson(DEFAULT_PATH+wid_file)
          for wid in wids:
               item=Widget(widget,wid[TYPE],wid[CONF])
               item.pack(wid[LAYOUT])


     def createCompentent(self,layout=None):
          for item in layout:
               wid=Widget(self.tk,item[TYPE],item[CONF])
               wid.pack(item[LAYOUT])
               self.CompententDetail(wid,item[FILE])
          body=Frame(self.tk,bg='red')
          bottom=LabelFrame(self.tk,bg='blue')
          
          text=Text(body)
          text.pack(fill=BOTH,expand = 1)
          
          label=Label(bottom,text='column:')
          label.pack(side=LEFT)
          label=Label(bottom,text='Line:')
          label.pack(side=LEFT)
          label=Label(bottom,text='Language')
          label.pack(side=RIGHT)
          label=Label(bottom,text='Tab Size:')
          label.pack(side=RIGHT)

          body.pack(fill = BOTH,expand = 1,side=TOP)
          body.pack_propagate(1)
          bottom.pack(fill = X,expand = 0,side = BOTTOM)
          


if __name__ == '__main__':
     t=View()
